package com.hp.ccue.crypto.util1;


import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.jasypt.salt.RandomSaltGenerator;
import org.jasypt.salt.SaltGenerator;

/**
 * class that is a PBE with MD5 and DES string encryptor with a standard
 * encryption key.
 */
public class PasswordEncryptor {

	public static String ALGORITHM = "PBEWithMD5AndDES";
	public static String ENCRYPTION_KEY = "gk8=347jG4;O<6";

	private SimpleStringPBEConfig config;
	private SimpleStringPBEConfig configPool;
	private StandardPBEStringEncryptor encryptor;
	private PooledPBEStringEncryptor pooledEncryptor;
	private SaltGenerator saltGenerator;

	public PasswordEncryptor() {
		this.config = new SimpleStringPBEConfig();
		this.configPool = new SimpleStringPBEConfig();
		this.pooledEncryptor = new PooledPBEStringEncryptor();
		this.encryptor = new StandardPBEStringEncryptor();
		this.saltGenerator = new RandomSaltGenerator();
		makeEncryptor();
	}

	/**
	 * Creates the actual encryptor given the defined properties
	 */
	private void makeEncryptor() {
		config.setAlgorithm(ALGORITHM);
		config.setPassword(ENCRYPTION_KEY);
		config.setSaltGenerator(saltGenerator);
		encryptor.setConfig(config);

		configPool.setAlgorithm(ALGORITHM);
		configPool.setPassword(ENCRYPTION_KEY);
		configPool.setSaltGenerator(saltGenerator);		
		pooledEncryptor.setConfig(configPool);
		pooledEncryptor.setPoolSize(100);
	}

	/**
	 * Returns the encryptor as a
	 * {@link org.jasypt.encryption.pbe.StandardPBEStringEncryptor}
	 *
	 * @return the {@link com.hp.ccue.crypto.util1.PasswordEncryptor} as a
	 *         {@link org.jasypt.encryption.pbe.StandardPBEStringEncryptor}
	 */
	public StandardPBEStringEncryptor getEncryptor() {
		return this.encryptor;
	}

	public PooledPBEStringEncryptor getPooledEncryptor() {
		return this.pooledEncryptor;
	}

}